/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import java.util.Locale;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspWriter;
import model.Employee;
import model.Position;
import session.EmployeeFacadeRemote;
import session.PositionFacadeRemote;
import util.Constants;

/**
 *
 * @author TIT
 */
public class profileController {

    public static EmployeeFacadeRemote getEmployeeFacade() throws NamingException {
        return util.Facade.getEmployeeFacade();
    }

    public static PositionFacadeRemote getPositionFacade() throws NamingException {
        return util.Facade.getPositionFacade();
    }

    public static String getActionLink(int id) {
        String edit = "<a onclick=\"doAction(frmIndex, '" + Constants.ADMIN_PAGE_NAME
                + "', '" + Constants.USER_MANAGEMENT_PAGE_NAME + "', '" + Constants.USER_EDIT
                + "', '" + Constants.SHOW_EDIT_PAGE + "', '" + id + "')\">Edit</a>";
        return edit;
    }

    public static int id() throws NamingException {
        int employid = 0;
        List<model.Employee> employeesList = getEmployeeFacade().findAll();
        for (int i = 0; i < employeesList.size(); i++) {
            Employee node = employeesList.get(i);
            if (node.getEmployeeID() > employid || node.getEmployeeID() == employid) {
                employid = node.getEmployeeID();
            }
        }
        return employid + 1;
    }

    public static void showAllEmployee(JspWriter out) {
        Controller.log.info("showAllUser");

        try {
            List<model.Employee> employeesList = getEmployeeFacade().findAll();

            //Create the title
            out.println("<div id='list-table'>");
            out.println("<div class='row'>");
            out.println("<div class='column-header' style='width: "
                    + Constants.ID_COLUMN_SIZE + ";'>ID</div>");
            out.println("<div class='column-header' style='width: "
                    + Constants.OBJECT_NAME_COLUMN_SIZE + ";'>Name</div>");
            out.println("<div class='column-header' style='width: "
                    + Constants.OBJECT_NAME_COLUMN_SIZE + ";'>ShortName</div>");
            out.println("<div class='column-header' style='width: "
                    + Constants.OBJECT_NAME_COLUMN_SIZE + ";'>Gender</div>");
            out.println("<div class='column-header' style='width: "
                    + Constants.OBJECT_NAME_COLUMN_SIZE + ";'>Bỉrth</div>");
            out.println("<div class='column-header' style='width: "
                    + Constants.OBJECT_NAME_COLUMN_SIZE + ";'>PersonalID</div>");
            out.println("<div class='column-header' style='width: "
                    + Constants.OBJECT_NAME_COLUMN_SIZE + ";'>Position</div>");
            out.println("<div class='column-header' style='width: "
                    + Constants.ACTION_COLUMN_SIZE + ";'>Action</div>");
            out.println("<div class='clear'></div>");
            out.println("</div>");

            //Show Values
            for (int i = 0; i < employeesList.size(); i++) {
                String gtt;
                Employee node = employeesList.get(i);
                Position position = node.getPositionID();
                int year = node.getBirth().getYear() + 1900;
                int month = node.getBirth().getMonth();
                if (node.getGender() == true) {
                    gtt = "Male";
                } else {
                    gtt = "Female";
                }
                out.println("<div class='row'>");
                out.println("<div class='column' style='width: "
                        + Constants.ID_COLUMN_SIZE + ";'>"
                        + node.getEmployeeID() + "</div>");
                out.println("<div class='column' style='width: "
                        + Constants.OBJECT_NAME_COLUMN_SIZE + ";'>"
                        + node.getName() + "</div>");
                out.println("<div class='column' style='width: "
                        + Constants.OBJECT_NAME_COLUMN_SIZE + ";'>"
                        + node.getShortName() + "</div>");
                out.println("<div class='column' style='width: "
                        + Constants.OBJECT_NAME_COLUMN_SIZE + ";'>"
                        + gtt + "</div>");
                out.println("<div class='column' style='width: "
                        + Constants.OBJECT_NAME_COLUMN_SIZE + ";'>"
                        + node.getBirth().getDate() + "/" + month + "/" + year + "</div>");
                out.println("<div class='column' style='width: "
                        + Constants.OBJECT_NAME_COLUMN_SIZE + ";'>"
                        + node.getPersonalID() + "</div>");
                out.println("<div class='column' style='width: "
                        + Constants.OBJECT_NAME_COLUMN_SIZE + ";'>"
                        + position.getName() + "</div>");
                out.println("<div class='column' style='width: "
                        + Constants.ACTION_COLUMN_SIZE + ";'>"
                        + getActionLink(node.getEmployeeID()) + "</div>");
                out.println("</div>");
                out.println("<div class='clear'></div>");
            }
            out.println("</div>");
            Controller.log.info("showAllEmployee finish.");
        } catch (Exception ex) {
            Controller.log.error("Error showAllEmployee: " + ex.getMessage());
        }
    }

    public static void addDataPosition(JspWriter out) {
        Controller.log.info("add Data Position");
        try {
            /*
             * Create Session Bean to do bussiness method
             */
            Context context = new InitialContext();
            Object ref = context.lookup("java:global/ejb-serv/PositionFacade");
            PositionFacadeRemote sevBean = (PositionFacadeRemote) ref;
            /*
             * Show position List on Screen
             */
            List<model.Position> listLog = sevBean.findAll();
            for (int i = 0; i < listLog.size(); i++) {
                Position a = listLog.get(i);
//                if(i==2)
//                    out.println("<option value='" + a.getPositionID() + "' selected='selected'>" + a.getName() + "</option>");
//                else
                out.println("<option value='" + a.getPositionID() + "'>" + a.getName() + "</option>");
            }
            Controller.log.info("show position finish.");
        } catch (Exception ex) {
            Controller.log.error("Error show posiotionl list : " + ex.getMessage());
        }
    }

    public static Date birth(String date, String month, String year) {
        Date birth;
        Calendar calendar = GregorianCalendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.set(Calendar.DATE, Integer.parseInt(date));
        calendar.set(Calendar.MONTH, Integer.parseInt(month));
        calendar.set(Calendar.YEAR, Integer.parseInt(year));
        birth = calendar.getTime();
        return birth;
    }

    public static Boolean gd(String gt) {
        if ("Female".equals(gt)) {
            return false;
        } else {
            return true;
        }
    }

    public static void processAction(HttpServletRequest request) {
        try {
            Controller.log.info("PROCESS ACTION PROFILE");
            String user = request.getParameter("user");
            Controller.log.info("user: " + user);
            EmployeeFacadeRemote employfacade = getEmployeeFacade();
            Employee employee = employfacade.find(Integer.parseInt(user));

            if (employee == null) {
                throw new Exception("The selected User not found.");
            } else {

                String gt = "Male";
                if (employee.getGender() == false) {
                    gt = "Female";
                }
                HttpSession session = request.getSession();
                session.setAttribute("employid", Integer.toString(employee.getEmployeeID()));
                session.setAttribute("name", employee.getName());
                session.setAttribute("shortname", employee.getShortName());
                session.setAttribute("date", Integer.toString(employee.getBirth().getDate()));
                session.setAttribute("month", Integer.toString(employee.getBirth().getMonth()));
                session.setAttribute("year", Integer.toString(employee.getBirth().getYear() + 1900));
                session.setAttribute("gender", gt);
                session.setAttribute("personalid", employee.getPersonalID());
                session.setAttribute("password", employee.getPassword());
                session.setAttribute("positionid", employee.getPositionID());
            }

        } catch (Exception ex) {
            // Controller.log.info(request.getParameter("birth"));
            Controller.log.error("Error EmployeeController.processAction: " + ex.getMessage());
        }
    }
}
